<template>
  <div class="bigbox">
   <!-- 页面导航搜索盒子 -->
    <div class="topbox">
      <el-form ref="form" label-position="left" label-width="60px">
          <el-row :gutter="20">
              <el-col :span="5">
                <el-form-item label="建筑群">
                  <FilterSelect2
                      v-model="buildingId"
                      :optionlist="qiyelist"
                      @updateModelValue="changeqiye"
                      :valuekey="{ name: 'name', value: 'id' }"
                    ></FilterSelect2>
                </el-form-item>
              </el-col>
              <el-col :span="5">
                <el-form-item label="用户">
                  <FilterSelect2
                      v-model="customerId"
                      :optionlist="yomghulist"
                      :valuekey="{ name: 'customerName', value: 'id' }"
                    ></FilterSelect2>
                </el-form-item>
              </el-col>
              <el-col :span="5">
                <el-form-item label="月份">
                  <el-date-picker
                  size="small"
                  style="width: 100%;"
                  value-format="yyyy-MM"
                    v-model="value2"
                    type="month"
                    placeholder="选择月">
                  </el-date-picker>
                </el-form-item>
              </el-col>
              <el-col :span="9" style="text-align: right;">
                <el-form-item label="">
                  <el-button  size="small" @click="chongzhi">重 置</el-button>
                   <el-button type="primary" size="small" @click="gettablelist()">查 询</el-button>
                </el-form-item>
              </el-col>
          </el-row>
      </el-form>
    </div> 
    <!-- 盒子主体部分 -->
    <div class="bodybox">
      <div class="title">
        <div>需量报表</div>
        <div><TopTile  @command="command" @daochu="daochu" @shuaxin="shuaxin"></TopTile></div>
      </div>
      <div class="table">
        <TableColumn
        ref="tabledata"
        :tableData="tableData" 
        :tableConfig="tableConfig" 
        :issize="issize"></TableColumn>
      </div>
        <!-- 底部分页 -->
        <div class="fenye">
          <el-pagination
                background
                layout="prev, pager, next"
                :current-page.sync="pageobj.pageNo"
                :page-size="20"
                @current-change="gettablelist"
                :total="+totalCount">
        </el-pagination>
      </div>
    </div>
  </div>
</template>

<script>
import {getxuliangapi} from '@/api/prwpaid/user-manage/abnormal-user'
import {gettablelistapi} from '@/api/prwpaid/user-manage/OpenAaccount'
import {getloginlistapi} from '@/api/prwpaid/file-management/Architectural-complex'
import TopTile from '@/components/TopTitle/index.vue'
import TableColumn from '@/components/Table/index.vue'
import {exportexcel} from '@/utils/excel'
export default {
  name: 'CallPoliceIndex',
  components: {
    TopTile,
    TableColumn
  },
  data() {
    return {
      resetFlag:'',
      issize: 'medium',
      buildingId: '',
      customerId:'',
      value2:'',
      yomghulist: [],
      qiyelist: [],
      tableData: [],
      tableConfig: [
      { label: '序号', type:'index' ,center:'center',width:'100'},
        { label: '园区/建筑群', prop: 'buildingsName',width:'200' },
        { label: '用户', prop: 'customerName' },
        { label: '月份', prop: 'month' },
        { label: '房间', prop: 'roomName' },
        { label: '计量点', prop: 'mpName' },
        { label: '最大需量(kW)', prop: 'maxDemandValue' },
        { label: '最大需量发送时间', prop: 'occurTime' ,center:'center'},
      ],
      pageobj: {
        pageNo: 1,
        pageSize:20
      },
      totalCount:''
    };
  },

  async mounted() {
    this.gettime()
    const res = await getloginlistapi()
    this.qiyelist = res.data.list
    const localbuildingId = localStorage.getItem('buildingId')
    const isBuildIdInArray = this.qiyelist.some(item => item.id == localbuildingId);
    if(isBuildIdInArray){
      this.buildingId = localbuildingId ? localbuildingId : ''
      this.getyonghuxialalist(this.buildingId)
    }else{
      this.buildingId = ''
    }

    this.gettablelist()
  },

  methods: {
    // 企业下拉
    changeqiye(val) {
      localStorage.setItem('buildingId', val)
      this.getyonghuxialalist(val)
    },
    // 获取用户
    async getyonghuxialalist(val) {
    const ress = await gettablelistapi({
        pageNo: 0, pageSize: 500,
      startDate: '', endDate: '', 
      state:'20',buildingId:val,customerType:''
    })
    this.yomghulist = ress.data.list
    },
    // 获取表格数据
    async gettablelist() {
      this.$refs.tabledata.loading=true
      const { data: { list,totalCount } } = await getxuliangapi({
        buildingId: this.buildingId, customerId: this.customerId,
        pageNo: this.pageobj.pageNo-1, pageSize: this.pageobj.pageSize,
        month:this.value2
      })
      this.tableData = list
      this.totalCount = totalCount
      this.$refs.tabledata.loading=false
    },
    // 重置
    chongzhi() {
      localStorage.removeItem('buildingId');
      this.resetFlag=''
      this.buildingId = ''
      this.customerId = ''
      this.gettime()
      this.gettablelist()
    },
 // 调节表格密度
  command(val) {
      if (val === '默认') {
        this.issize = 'medium'
      } else if (val === '中等') {
        this.issize = 'small'
      } else {
        this.issize = 'mini'
      }
    },
    // 导出
    daochu() {
      exportexcel('mbm-microservice-biz/mbm/report/demand/exportExcel',{
        buildingId: this.buildingId, customerId: this.customerId,
        pageNo: this.pageobj.pageNo-1, pageSize: this.pageobj.pageSize,
        month:this.value2
      },'需量报表')
    },
    // 刷新列表
    shuaxin() {
      this.gettablelist()
      this.$message.success('列表刷新成功')
    },
    // 获取当前时间戳
    gettime() {
      const date = new Date()
      const year = date.getFullYear() // 年
      const mon = date.getMonth() + 1 // 月
      const month =mon<10?'0'+mon:mon
      this.value2 = year + '-' + month
    },
  },
};
</script>

<style lang="scss" scoped>
.option{
  padding-left: 10px;
}
.bigbox{
  height: 100%;
  padding: 10px;
  .topbox{
    height: 80px;
    padding: 20px;
    background-color: #fff;
    margin-bottom: 10px;
  }
  .bodybox{
    height: calc(100% - 90px);
    padding: 20px;
    background-color: #fff;
    .title{
      margin-bottom: 10px;
      display:flex;
      justify-content:space-between;
    }
    .table{
      height: 90%;
    }
    .fenye{
      text-align: right;
    }
  }
}
</style>